package cn.exrick.xboot.modules.information.controller;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.information.entity.Information;
import cn.exrick.xboot.modules.information.service.IInformationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "新消息通知管理接口")
@RequestMapping("/xboot/information")
@Transactional
public class InformationController {

    @Autowired
    private IInformationService iInformationService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Information> get(@PathVariable String id){

        Information information = iInformationService.getById(id);
        return new ResultUtil<Information>().setData(information);
    }

    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Information> getById(@Param("id") String id){

        Information information = iInformationService.getById(id);
        return new ResultUtil<Information>().setData(information);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Information>> getAll(){

        List<Information> list = iInformationService.list();
        return new ResultUtil<List<Information>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Information>> getByPage(@ModelAttribute PageVo page){

        IPage<Information> data = iInformationService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Information>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Information> saveOrUpdate(@ModelAttribute Information information){
        information.setUpdateTime(new Date());
        if(iInformationService.saveOrUpdate(information)){
            return new ResultUtil<Information>().setData(information);
        }
        return new ResultUtil<Information>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iInformationService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
    @RequestMapping(value = "/selectInformationPageVoMap", method = RequestMethod.GET)
    @ApiOperation(value = "新消息通知条件分页获取数据")
    public Result<IPage<Information>> selectInformationPageVoMap(@ModelAttribute Page page, @Param("title") String title, @Param("department") String department) {

        Map<String, Object> map = new HashMap<String, Object>();
        if(title!=null&&!title.equals("")&&title.length()!=0&&!title.trim().isEmpty()){
            map.put("title",title);
        }
        if(department!=null&&!department.equals("")&&department.length()!=0&&!department.trim().isEmpty()){
            map.put("department", department);
        }
        IPage<Information> informationIPage = iInformationService.selectInformationPageVoMap(page, map);
        return new ResultUtil<IPage<Information>>().setData(informationIPage);
    }

    @RequestMapping(value = "/getAppInformationPage", method = RequestMethod.GET)
    @ApiOperation(value = "app新消息通知条件分页获取数据，返回单点地址")
    public Result<IPage<Information>> selectAppInformationPageVoMap(@ModelAttribute Page page, @Param("title") String title, @Param("department") String department) {

        Map<String, Object> map = new HashMap<String, Object>();
        if(title!=null&&!title.equals("")&&title.length()!=0&&!title.trim().isEmpty()){
            map.put("title",title);
        }
        if(department!=null&&!department.equals("")&&department.length()!=0&&!department.trim().isEmpty()){
            map.put("department", department);
        }
        map.put("dictType", "newInfo");
        IPage<Information> informationIPage = iInformationService.selectAppInformationPageVoMap(page, map);
        return new ResultUtil<IPage<Information>>().setData(informationIPage);
    }
    @RequestMapping(value = "/addInformation", method = RequestMethod.POST)
    @ApiOperation(value = "新增保存数据")
    public Result<Object> addInformation(@ModelAttribute Information information) {
        information.setInfoCode(CommonConstant.BDinfoCode);
        information.setSource(CommonConstant.SOURCE);
        information.setCreateTime(new Date());
        information.setUpdateTime(new Date());
        boolean save = iInformationService.save(information);
        if(save){
            return new ResultUtil<Object>().setSuccessMsg("添加成功");
        }
        return new ResultUtil<Object>().setErrorMsg("添加失败");
    }
}
